package com.example.demo.controller;

import com.example.demo.config.ResponseModel;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户controller",tags = "用户操作接口")
public class UserController {

    @Autowired
    private UserService userService;



    @GetMapping("/queryUserList")
    @ResponseBody
    @Cacheable(cacheNames = "userList", key = "'queryUserList'")
    @ApiOperation(value="查询所有用户",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "分页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "行数",dataType = "Integer",paramType = "query")})
    public ResponseModel queryUserList(@RequestParam("page") Integer page,
                                       @RequestParam("limit") Integer limit){
        Integer index = (page-1)*limit;
        List<User> userList = userService.queryUserList(index,limit);
        return new ResponseModel(userList,"","200");
    }

//    @GetMapping("/findById/{id}")
//    public User findById(@PathVariable("id") long id){
//        return userMapper.findById(id);
//    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value="根据id查询用户",httpMethod = "GET")
    @ApiImplicitParam(name = "id",value = "用户Id",dataType = "Long",paramType = "query")
    public User findById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

//    public ModelAndView findById(@PathVariable("id") long id){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("user_update");
//        modelAndView.addObject("user",userService.findById(id));
//        return modelAndView;
//    }

    //修改用户信息
    @PostMapping("/admin/updateUser")
    @ApiOperation(value="修改用户",httpMethod = "POST")
    public ResponseModel updateUser(User user){
        Boolean flag = userService.updateUser(user)>0;
        if(flag){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    //删除用户
    @DeleteMapping("/admin/deleteUser/{id}")
    @ApiOperation(value="根据id删除用户",httpMethod = "DELETE")
    @ApiImplicitParam(name = "id",value = "用户Id",dataType = "Long",paramType = "query")
    public ResponseModel deleteUser(@PathVariable("id") Long id){
        Boolean flag = userService.deleteUser(id)>0;
        if(flag){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }
}
